package student.list.update;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.MysqlMethod;
import student.list.studentlist.StudentListView;
import student.list.updateStudent.SelectStudentInfo;
import student.list.updateStudent.SelectStudentList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
@WebServlet("/student1")
public class StudentAddServlet extends HttpServlet {
    @Override
    /**
     * 添加学生信息的方法
     */
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");//转码
        //获取当前时间
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr =  format.format(date);
        //创建对象
        StudentListView studentListView = new StudentListView();
        studentListView.setCode(0);//code为0说明请求成功
        studentListView.setMsg("success");
        studentListView.setTotal(10);//设置要传递的参数的个数
        //获取前端发送过来的数据
        String age = req.getParameter("age");
        //将获取的age转换为Interger类型
        Integer intage = null;
        if(age!=null){
            intage = Integer.valueOf(age);
        }
        //System.out.println(intage);
        String classId = req.getParameter("classId");
        Integer intclassId = null;
        //将获取的classId转换为Interger类型
        if(classId!=null){
            intclassId = Integer.valueOf(classId);
        }
        String className = req.getParameter("className");
        System.out.println(className);
        String name = req.getParameter("name");
        //System.out.println(name);
        String phone = req.getParameter("phone");
        String remark = req.getParameter("remark");
        String sex = req.getParameter("sex");
        String studentId = req.getParameter("studentId");
        Integer intstudentId = null;
        //将获取的studentId转换为Interger类型
        if(studentId!=null){
            intstudentId = Integer.valueOf(studentId);
        }
        String studentNo = req.getParameter("studentNo");
        //将获取的值传入数据库进行添加
        MysqlMethod.addevent(intage,intclassId,className,dateStr,name,phone,remark,sex,intstudentId,studentNo);
    }

    /**
     * 删除学生信息的方法
     */
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端传过来的studentId
        String  studentId1 = req.getParameter("studentId");
        int studentId = Integer.parseInt(studentId1);
        //调用方法删除学生信息
        MysqlMethod.delete01(studentId);
    }

    /**
     修改学生信息的方法
     */
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //在doPUt中要用流的方法获取前端传过来的参数
        BufferedReader reader = req.getReader();
        String studentId = reader.readLine();
        reader.close();//关闭流
        String str1 = URLDecoder.decode(studentId,"utf-8");//将获取的数据进行转码
        String[] str =  MysqlMethod.split02(str1);//将获取的字符串进行拆分得到数据
        //将获取到String类型的数据转为Interget类型
        Integer strage = Integer.valueOf(str[0]);
        Integer strclassId = Integer.valueOf(str[1]);
        Integer strstudentId = Integer.valueOf(str[8]);
        //调用方法进行修改
        MysqlMethod.update01(strage,strclassId,str[2],str[3],str[4],str[5],str[6],str[7],strstudentId,str[9]);
    }

    /**
     * 通过前端传过来的参数查询数据库显示在前端用于修改
     */
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");//转码
        SelectStudentList selectStudentList = new SelectStudentList();
        selectStudentList.setCode(0);//设置code值,0代表请求成功
        selectStudentList.setMsg("success");//设置MSg的值
        //获取前端传过来的studentId
        String  studentId1 = req.getParameter("studentId");
        int studentId = Integer.parseInt(studentId1);
        String[] a = MysqlMethod.selectSingle(studentId);//查询数据库,获取数据
        //数据类型转换
        Integer a1 = Integer.valueOf(a[0]);
        Integer a2 = Integer.valueOf(a[1]);
        Integer a3 = Integer.valueOf(a[8]);
        //给selectStudentInfo对象赋初值
        SelectStudentInfo selectStudentInfo = new SelectStudentInfo(a1,a2,a[2],a[3],a[4],a[5],a[6],a[7],a3,a[9]);
        //把selectStudentInfo的值加入selectStudentList(selectStudentInfo是selectStudentList的一个属性,所以也要把selectStudentInfo的值赋给selectStudentList)
        selectStudentList.setData(selectStudentInfo);
        //将selectStudentList转为json格式
        ObjectMapper objectMapper = new ObjectMapper();
        String result = objectMapper.writeValueAsString(selectStudentList);
        //向前端传值
        resp.getWriter().write(result);

    }

}
